<template>
	<view class="main">
		<guide v-if="guidePages" :advData="advData" :time="advData.time" @closeGuideBanner="closeGuideBanner"></guide>
		<!-- <LabelList v-else-if="!guidePages && label_type == 1" :list="lable_list" title="目标客户行业" label_length="3" @submitLabel="submitLabel"></LabelList>
		<LabelList v-else-if="!guidePages && label_type == 2" :list="lable_list" title="我从事的行业" label_length="1" @submitLabel="submitLabel"></LabelList> -->
	</view>
</template>

<script>
	import guide from '@/components/guide/index.vue';
	import LabelList from '@/components/labelList/index.vue';
	import Cache from '@/utils/cache';
	import {
		getSystemGroupData,
		getLabelList
	} from '@/api/api.js'
	import Routine from '@/libs/routine.js';
	export default {
		components: {
			guide,
			LabelList,
		},
		data() {
			return {
				guidePages: false,
				advData: [],
				label_type: 0,
				lable_list: [],
				link_url: '',
			}
		},
		async onShow() {
			let that = this;
			await this.getLabel();
			if(!this.$Cache.get('engagedIndustry')){
				Routine.getCode().then(res=>{
					Routine.authCodeUserLabelId({code: res}).then(res2=>{
						// console.log('res2', res2);
						if(res2.data && res2.data.label_id){
							that.$Cache.set('customerIndustry', res2.data.customer_industry);
							that.$Cache.set('engagedIndustry', res2.data.label_id);
						}else{
							that.label_type = 1;
						}
					}).catch(err2=>{
						console.log('err2', err2);
						that.label_type = 1;
					})
				}).catch(err=>{
					console.log('err', err);
					that.label_type = 1;
				})
			}
			await this.loadExecution();
		},
		methods: {
			getLabel() {
				let that = this;
				getLabelList({}).then(res=>{
					// console.log('res', res);
					that.lable_list = res.data.list;
				}).catch(err=>{
					console.log('err', err);
				})
			},
			// 通用函数：随机选择 count 个不重复的 label_id
			getRandomLabelIds(data, count) {
			  if (count > data.length) {
			    throw new Error("选择的数量不能超过数组长度");
			  }
			  const indices = new Set();
			  while (indices.size < count) {
			    const randomIndex = Math.floor(Math.random() * data.length);
			    indices.add(randomIndex);
			  }
			  return Array.from(indices).map(index => data[index].label_id);
			},
			closeGuideBanner(res) {
				let that = this;
				if(this.$Cache.get('engagedIndustry')){
					return that.jumpToPage();
				}else{
					let data1 = that.getRandomLabelIds(that.lable_list, 1);
					that.$Cache.set('engagedIndustry', data1[0]);
					let data2 = that.getRandomLabelIds(that.lable_list, 3);
					that.$Cache.set('customerIndustry', data2.join(','));
					that.jumpToPage();
					/* uni.setNavigationBarTitle({
					    title: "目标客户行业"
					});
					// 关闭banner展示
					that.label_type = 1;
					that.guidePages = false; */
				}
			},
			submitLabel(res) {
				let that = this;
				if(that.label_type == 1){
					uni.setNavigationBarTitle({
					    title: "我从事的行业"
					});
					that.label_type = 2;
					// 目标客户行业
					that.$Cache.set('customerIndustry', res.join(','));
					return false;
				}else if(that.label_type == 2){
					// 我从事的行业
					that.$Cache.set('engagedIndustry', res[0]);
				}
				that.jumpToPage();
			},
			loadExecution() {
				const that = this,
					tagDate = uni.getStorageSync('guideDate') || '',
					nowDate = new Date().toLocaleDateString();
				if (tagDate === nowDate) {
					/* uni.switchTab({
						url: '/pages/index/index'
					}); */
					that.closeGuideBanner({url:''})
					return
				}
				getSystemGroupData({name: 'open_screen_advertising'}).then(res => {
					if (res.data.status == 0 || res.data.value.length == 0) {
						// uni.switchTab({
						// 	url: '/pages/index/index'
						// });
						that.closeGuideBanner({url:''})
					} else if (res.data.status && (res.data.value.length || res.data.data.video_link)) {
						this.advData = res.data
						uni.setStorageSync('guideDate', new Date().toLocaleDateString());
						this.guidePages = true
					}
				}).catch(err => {
					// uni.switchTab({
					// 	url: '/pages/index/index'
					// });
					that.closeGuideBanner({url:''})
				})
			},
			jumpToPage() {
				let link_url = this.link_url;
				if(link_url){
					if (link_url.indexOf("http") != -1) {
						uni.navigateTo({
							url: `/pages/annex/web_view/index?url=${link_url}`
						});
					} else {
						uni.reLaunch({
							link_url: link_url,
							fail: () => {
								uni.switchTab({
									link_url
								})
							}
						})
					}
				}else{
					uni.switchTab({
						url: '/pages/index/index'
					});
				}
			}
		},
		onHide() {
			this.closeGuideBanner({url:''});
		}
	}
</script>

<style>
	page,
	.main {
		width: 100%;
		height: 100%;
	}
</style>
